package com.aerozhonghuan.logisticsmall.framework.base;

import android.support.multidex.MultiDexApplication;
import android.util.Log;

import com.aerozhonghuan.foundation.eventbus.EventBusManager;
import com.aerozhonghuan.foundation.log.LogUtil;
import com.aerozhonghuan.foundation.umeng.UmengAgent;
import com.aerozhonghuan.hybrid.HybridContext;
import com.aerozhonghuan.logisticsmall.BuildConfig;
import com.aerozhonghuan.logisticsmall.R;
import com.aerozhonghuan.logisticsmall.framework.hybrid.HybridHttpProxy;
import com.aerozhonghuan.logisticsmall.framework.logback.LogConfigurator;
import com.aerozhonghuan.logisticsmall.framework.logback.LogbackAppender;
import com.aerozhonghuan.logisticsmall.utils.ProcessUtil;
import com.aerozhonghuan.oknet2.LogInterceptor;
import com.aerozhonghuan.oknet2.OknetConfig;
import com.aerozhonghuan.zh_map.ZHSDKInitializer;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.LogUtils;
import com.bumptech.glide.request.target.ViewTarget;

/**
 * MyAppliation
 * Created by zhangyunfei on 17/6/19.
 */

public class MyAppliation extends MultiDexApplication {
    private static final String TAG = "MyAppliation";
    private static MyAppliation instance;

    public static MyAppliation getApplication() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        ViewTarget.setTagId(R.id.tag_glide);
        LogUtils.getConfig().setLogSwitch(AppUtils.isAppDebug()).setGlobalTag("hhh");
        instance = this;
        ZHSDKInitializer.init(this);
        initGlobalSettings();
        //默认异常处理
        CrashExceptionHandler.setDefaultUncaughtExceptionHandler(this);
        //开发阶段的特殊配置
        justRunOnDevelop();
    }

    /**
     * 初始全局设定,针对模块的设定不应该写在这里
     * 在单个进程仅初始化一次。防止 多进程应用时，this application 创建多次
     */
    private void initGlobalSettings() {
        String processName = ProcessUtil.getProcessName(android.os.Process.myPid());
        //检查当前是否是同包名相同的进程，如果是，则为主进程，执行设定
        if (!getPackageName().equals(processName)) {
            return;
        }
        LogUtil.d(TAG, "################################");
        LogUtil.d(TAG, "程序启动");
        //日志基础功能优先处理
        LogUtil.isEnable = BuildConfig.DEBUG;
        OknetConfig.DEBUG = BuildConfig.DEBUG;
        EventBusManager.DEBUG = false;
        initLogback();//初始化日志框架
        LogUtil.d(TAG, "初始化uemng统计框架 完成");
        initOknet();//初始化网络
        LogUtil.d(TAG, "初始化网络框架 完成");
        LogUtil.d(TAG, "初始化 Hybrid HTTP 代理对象 完成");
        HybridContext.setHttpProxy(new HybridHttpProxy());
    }

    /**
     * 开发阶段的特殊配置
     * 强制关闭 某些 外部sdk注册的 UncaughtExceptionHandler
     */
    private void justRunOnDevelop() {
        //如果app编译为开发者模式，则关闭umeng统计，开启umeng debug
        if (BuildConfig.BUILD_TYPE.equals("debug")) {
            LogUtil.d(TAG, "umeng统计，关闭捕获异常");
            UmengAgent.setCatchUncaughtExceptions(false);
            UmengAgent.setDebugMode(true);

            //阻止友盟捕获异常导致app重启
            Thread.setDefaultUncaughtExceptionHandler(null);
            Thread.UncaughtExceptionHandler defaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
            if (defaultUncaughtExceptionHandler != null) {
                String str = defaultUncaughtExceptionHandler.getClass().toString();
                LogUtil.d(TAG, "发现某些 外部sdk注册的UncaughtExceptionHandler： " + str);
            }
        }
    }

    private void initLogback() {
        if (LogUtil.isEnable) {
            LogConfigurator.confifure();
            LogUtil.getAppenderGroup().clear();
            LogUtil.getAppenderGroup().addChildLogger(new LogbackAppender());
            LogUtil.d(TAG, "初始化日志框架 完成");
        }
    }

    private void initOknet() {
        //配置okhttp 缓存位置
        OknetConfig.setExternalCacheDir(getExternalCacheDir());
        OknetConfig.setRequestParaInterceptor(new CustomRequestParaInterceptor());
        OknetConfig.setDefaultExceptionHandler(new CustomDefalutExceptionHandler());
        OknetConfig.setLogInterceptor(new LogInterceptor() {
            @Override
            public void d(String tag, String msg) {
                Log.i(tag, msg);
            }

            @Override
            public void i(String tag, String msg) {
                Log.d(tag, msg);
            }

            @Override
            public void e(String tag, String msg) {
                Log.e(tag, msg);
            }
        });
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
    }

}
